<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
	<title></title>
	<%@include file="/view/resource.jsp" %>
	<link rel="stylesheet" type="text/css" href="${ctx}/css/base.css">
</head>
<body>
<style type="text/css">
	.buttonSend {
		background-color: #009688;
		color: #fff;
		cursor: pointer;
		height: 35px;
		border: none;
		min-width: 80px;
	}

	.buttonSend:hover {
		color: #000;
	}
</style>
<form id="entityForm" name="entityForm" action="${ctx}/terminalCommand/callTerminal"
      method="POST">
	<input type="hidden" name="dvcId" id="dvcId" value='${dvcId}'/>
	<table class="editTable">
		<%--	<tr>
			<td colspan="6" style="font-weight: bold;" height="15">
				<span class="commandMsg">开始发送点名命令</span>
			</td>
		</tr>
		<tr>
				<td colspan="6" style="font-weight: bold;" height="35" align="center">
					<input id="btnSend" class="buttonSend" type="button" value="重新执行" onclick="reloadSend();"/>
				</td>
			</tr>--%>
	</table>
</form>
<script type="text/javascript" src="${ctx}/js/common/package.js"></script>
<script type="text/javascript" src="${ctx}/js/common/base.js?v=11"></script>
<script type="text/javascript" src="${ctx}/js/jquery/jquery.timers.js"></script>
<!--定时器-->
<script type="text/javascript" src="${ctx}/js/terminalCommand/terminalCommand.js"></script>
<!--终端命令结果查询-->
<script type="text/javascript" charset="utf-8">
	var dvcId = ${dvcId};
	//查询命令执行结果
	function onCommandSuccess(result) {
		//停止所有的在$('body')上定时器
		$('body').stopTime();
		$(".commandMsg").html("命令执行结果:" + result.msg);
		if (result.msg && result.msg == "命令执行成功") {
			//alert(result.message);
			MyMap.showVehicleOnMap(dvcId);
			TerminalCommand.close();
		} else {
			//$("#btnSend").show();
		}
	}


	function reloadSend() {
		var data = {};
		Bdth.submitForm($("#entityForm"), data, function (result) {
			if (result.success) {
				//alert("命令已经下发,请等待处理!");
				//var commandId = result.dvcId; //下发成功后，获取到命令Id
				//TerminalCommand.times = 100;
				//TerminalCommand.startQueryResult(commandId, null, onCommandSuccess); //命令下发成功,根据命令id,开始尝试获取检索结果
				var zTree = $.fn.zTree.getZTreeObj("deviceTree");
				var node = zTree.getNodeByParam("id", "v" + dvcId);
				var deviceId = node.id.substring(1, node.id.length);
				zTree.checkNode(node, true, true);//指定选中ID的节点
				var ztree1 = $.fn.zTree.getZTreeObj("deviceTree"), nodes = ztree1.getCheckedNodes(true);
				var selectDeviceIds = "";
				if (nodes && nodes.length) {
					for (var i = 0, len = nodes.length; i < len; i++) {
						var node = nodes[i];
						if (typeof (node.children) == "undefined" && node.id.indexOf("v") == 0) {
							nodeId = node.id.substring(1, node.id.length)
							selectDeviceIds += nodeId + ",";
						}
					}
					if (selectDeviceIds != "") {
						selectDeviceIds = selectDeviceIds.substring(0, selectDeviceIds.length - 1);
					}

					selectDeviceIds = selectDeviceIds;
					RealDataGrid.selectDeviceIds = selectDeviceIds;
				}
				var url = path + "/realData/registerDevicesBatch";
				var params = {registerDeviceIds: selectDeviceIds};
				$.ajax({
					type: "POST",
					url: url,
					data: params,
					error: function () {
						//alert("网络连接错误，无法读取数据!");
						//Utility.done();
					},
					success: function (data) {
						//刷新实时表格数据
						RealDataGrid.refresh(false);
					}
				});
				MyMap.showVehicleOnMap(dvcId);
				TerminalCommand.close();
			}
			else {
				$(".commandMsg").html("提交失败!" + (result.msg ? result.msg : result.data));
				//停止所有的在$('body')上定时器
				//$('body').stopTime();
			}
		});
	}

	$(document).ready(function () {
		/*		var option =
		 {
		 type: 'post',
		 dataType: 'json',
		 success: function (result) {
		 if (result.success) {
		 //alert("命令已经下发,请等待处理!");
		 var commandId = result.data; //下发成功后，获取到命令Id
		 TerminalCommand.times = 100;
		 TerminalCommand.startQueryResult(commandId, null, onCommandSuccess); //命令下发成功,根据命令id,开始尝试获取检索结果
		 }
		 else {
		 $(".commandMsg").html("提交失败! 错误原因：" + (result.msg ? result.msg : result.data));
		 //停止所有的在$('body')上定时器
		 $('body').stopTime();
		 }
		 },
		 error: function (response, textStatus, errorThrown) {
		 try {
		 $.iMessager.alert('提示', result.msg || "请求出现异常", 'messager-error');
		 } catch (e) {
		 $.iMessager.alert('提示', "请求出现异常.", 'messager-error');
		 }
		 },
		 complete: function () {

		 }
		 }


		 $("#entityForm").ajaxSubmit(option);*/
		//$("#btnReload").hide();

		var data = {};
		Bdth.submitForm($("#entityForm"), data, function (result) {
			if (result.success) {
				//alert("命令已经下发,请等待处理!");
				//var commandId = result.dvcId; //下发成功后，获取到命令Id
				//TerminalCommand.times = 100;
				//TerminalCommand.startQueryResult(commandId, null, onCommandSuccess); //命令下发成功,根据命令id,开始尝试获取检索结果
				var zTree = $.fn.zTree.getZTreeObj("deviceTree");
				var node = zTree.getNodeByParam("id", "v" + dvcId);
				var deviceId = node.id.substring(1, node.id.length);
				zTree.checkNode(node, true, true);//指定选中ID的节点
				var ztree1 = $.fn.zTree.getZTreeObj("deviceTree"), nodes = ztree1.getCheckedNodes(true);
				var selectDeviceIds = "";
				if (nodes && nodes.length) {
					for (var i = 0, len = nodes.length; i < len; i++) {
						var node = nodes[i];
						if (typeof (node.children) == "undefined" && node.id.indexOf("v") == 0) {
							nodeId = node.id.substring(1, node.id.length)
							selectDeviceIds += nodeId + ",";
						}
					}
					if (selectDeviceIds != "") {
						selectDeviceIds = selectDeviceIds.substring(0, selectDeviceIds.length - 1);
					}

					selectDeviceIds = selectDeviceIds;
					RealDataGrid.selectDeviceIds = selectDeviceIds;
				}
				var url = path + "/realData/registerDevicesBatch";
				var params = {registerDeviceIds: selectDeviceIds};
				$.ajax({
					type: "POST",
					url: url,
					data: params,
					error: function () {
						//alert("网络连接错误，无法读取数据!");
						//Utility.done();
					},
					success: function (data) {
						//刷新实时表格数据
						RealDataGrid.refresh(false);
					}
				});
				MyMap.showVehicleOnMap(dvcId);
				TerminalCommand.close();
			}
			else {
				//$(".commandMsg").html("提交失败!" + (result.msg ? result.msg : result.data));
				TerminalCommand.close();
				$.iMessager.alert('提示', "提交失败!" + result.msg, 'messager-error');
				//停止所有的在$('body')上定时器
				//$('body').stopTime();
			}
		});

	});
</script>
</body>
</html>
